﻿namespace Web.Control.WWW.Partner
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;

    using Lx.Web.Common.ControlsUI;    
    using System.Collections.Generic;
    using System.Reflection;
    /// <summary>
    /// SearchPartner 的摘要说明
    /// </summary>
    public class Search : BaseControl
    {
        /// <summary>
        /// 当前会员号
        /// </summary>
        string memberno = "";
        /// <summary>
        /// 学习语言
        /// </summary>
        protected string LL = "";
        /// <summary>
        /// 使用语言
        /// </summary>
        protected string BL = "";
        /// <summary>
        /// 国籍
        /// </summary>
        protected string Nationality = "";
        /// <summary>
        /// 最大年龄
        /// </summary>
        protected string MaxAge = "200";
        /// <summary>
        /// 最小年龄
        /// </summary>
        protected string MinAge = "0";
        /// <summary>
        /// 性别
        /// </summary>
        protected string Gender = "";
        /// <summary>
        /// 是否只显示有图像的会员0否、1是
        /// </summary>
        protected string IsAvatar = "0";
        /// <summary>
        /// 是否只显示母语1为BL的会员0否、1是
        /// </summary>
        protected string IsNativeBL = "0";        
        /// <summary>
        /// 详细列表大小
        /// </summary>
        protected Literal litCount;        
        protected Repeater rptUserList;        
        protected Repeater rptPopular;
        protected int RecordCount = 0;
        protected int PageIndex = 1;
        protected int psize = 20;
        /// <summary>
        /// 根据 昵称名或会员email或会员号搜索
        /// </summary>
        protected string Name_F = "";

        protected string C0003 = "";
        protected string C0005 = "";
        protected string C0007 = "";
        protected string C0009 = "";
        protected override void OnLoad(EventArgs e)
        {
            C0003 = GetText("C0003");
            C0005 = GetText("C0005");
            C0007 = GetText("C0007");
            C0009 = GetText("C0009");

            ArrayList arrList = new ArrayList();
            memberno = HttpContext.Current.User.Identity.Name;
            //读取cookie值
            if (Request.Cookies["italkiLearn"] != null)
                LL = Request.Cookies["italkiLearn"].Value == "all" ? "" : Request.Cookies["italkiLearn"].Value;
            if (Request.Cookies["italkiSpeak"] != null)
                LL = Request.Cookies["italkiSpeak"].Value == "all" ? "" : Request.Cookies["italkiSpeak"].Value;
           
                if (!string.IsNullOrEmpty(Request["p"]))
                {
                    if (Request["p"] == "L")
                    {
                        PageIndex = 999;
                    }
                    else
                    {
                        int.TryParse(Request["p"], out PageIndex);
                    }
                }
                if (!string.IsNullOrEmpty(Request["name"]))
                {
                    Name_F = Request["name"];
                }
                else
                {
                    if (Request["ll"]!=null)
                    {
                        LL = Request["ll"];
                        HttpCookie llCookie = new HttpCookie("italkiLearn", LL);//保存cookie
                        Response.Cookies.Add(llCookie);
                    }
                    if (Request["bl"]!=null)
                    {
                        BL = Request["bl"];
                        HttpCookie blCookie = new HttpCookie("italkiSpeak", BL);
                        Response.Cookies.Add(blCookie);
                    }
                    if (!string.IsNullOrEmpty(Request["nation"]))
                    {
                        Nationality = Request["nation"];
                    }
                    if (!string.IsNullOrEmpty(Request["max"]))
                    {
                        MaxAge = Request["max"];
                    }
                    if (!string.IsNullOrEmpty(Request["min"]))
                    {
                        MinAge = Request["min"];
                    }
                    if (!string.IsNullOrEmpty(Request["gen"]))
                    {
                        Gender = Request["gen"];
                    }
                    if (!string.IsNullOrEmpty(Request["isa"]))
                    {
                        IsAvatar = Request["isa"];
                    }
                    if (!string.IsNullOrEmpty(Request["isbl"]))
                    {
                        IsNativeBL = Request["isbl"];
                    }
                }
            
            int.TryParse(litCount.Text, out psize);
            if (Name_F != "")
                arrList = BLF.WWW_WWW.Contact.Contacts.GetFindPartner_MN(Name_F, memberno, PageIndex, psize);           
            else
                arrList = BLF.WWW_WWW.Contact.Contacts.GetFindPartner(memberno, LL, BL, Nationality, MaxAge, MinAge, Gender, IsAvatar == "1" ? true : false, IsNativeBL == "1" ? true : false, PageIndex, psize);
            if (arrList != null)
            {
                RecordCount = (int)arrList[0];
                int pCount = (int)Math.Ceiling(((decimal)RecordCount) / ((decimal)psize));
                if (PageIndex > pCount) PageIndex = pCount;

                if (rptUserList != null)
                {
                    rptUserList.DataSource = arrList[1];
                    rptUserList.DataBind();
                }
                if (rptPopular != null)
                {
                    List<BLF.LanguageObj> lanObjList = new List<BLF.LanguageObj>();
                    lanObjList.AddRange(BLF.StaticData.GetPopLanguageList);
                    rptPopular.DataSource = lanObjList;
                    rptPopular.DataBind();
                }
                //title
                this.DynamicDescription = GetText("Find a Language Partner for language exchange, or find a language teacher at italki");
                if (!string.IsNullOrEmpty(Request["ll"]) && !string.IsNullOrEmpty(Request["bl"]) && Request["nation"] == null)
                {
                    this.DynamicTitle = GetText("Find a Language Exchange Partner - speak {0}, learn {1} - italki", new string[] { BLF.StaticData.GetLanguageText(BL), BLF.StaticData.GetLanguageText(LL) });
                    this.DynamicKeywords = GetText("language exchange, {0}, {1}", new string[] { BLF.StaticData.GetLanguageText(BL), BLF.StaticData.GetLanguageText(LL) });
                }
                else if (!string.IsNullOrEmpty(Request["bl"]) && Request["nation"] == null)
                {
                    this.DynamicTitle = GetText("Find a Language Exchange Partner - speak {0} - italki", new string[] { BLF.StaticData.GetLanguageText(BL) });
                    this.DynamicKeywords = GetText("language exchange, {0}", new string[] { BLF.StaticData.GetLanguageText(BL) });
                }
                else if (!string.IsNullOrEmpty(Request["ll"]) && Request["nation"] == null)
                {
                    this.DynamicTitle = GetText("Find a Language Exchange Partner - learn {0} - italki", new string[] { BLF.StaticData.GetLanguageText(LL) });
                    this.DynamicKeywords = GetText("language exchange, {0}", new string[] { BLF.StaticData.GetLanguageText(LL) });
                }
                else if (!string.IsNullOrEmpty(Request["nation"]))
                {
                    this.DynamicTitle = GetText("Find a Language Exchange Partner - form {0} - italki", new string[] { BLF.StaticData.GetCountryText(Nationality) });
                    this.DynamicKeywords = GetText("language exchage, {0}", new string[] { BLF.StaticData.GetCountryText(Nationality) });
                }
                else
                {
                    this.DynamicTitle = GetText("Find a Language Exchange Partner - italki");
                    this.DynamicKeywords = GetText("language exchange");
                }
            }
            base.OnLoad(e);
        }       
    }
}
